﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Rights_CanAccessByPosition_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Rights_CanAccessByPosition_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Rights_CanAccessByPosition_V2]
    @ClassID    int,
    @StaffID    int,
    @Act_ID     int,
    @AllowAccess bit output
/*

======================================================================
功能:    得到职位中@UserName是否能对@ClassID组进行@Act_ID操作
参数:
    @ClassID int    :    组（类）ID
    @StaffID int    :    用户编号
    @Act_ID int     :    权利代号

备注:    继承指是否继承在父组中的权利(在父组中有权利即在此组中有权利)
======================================================================

*/
AS
BEGIN
SET NOCOUNT ON

SET @AllowAccess = 0;

--得到所有父组中拥有此权利的职位ID
DECLARE @pAssingPosition TABLE (Positionid int);
INSERT INTO @pAssingPosition 
    SELECT Position_ID 
        FROM 
            dbo.UDS_Assign_Rule A
            INNER JOIN dbo.UDS_Class C ON A.Team_ID = C.ClassID
        WHERE
            A.Based_ON = 1 /* position */
            AND A.Act_ID = @Act_id
            AND C.ClassID IN (
                SELECT ClassID FROM dbo.fn_get_parent_class(@ClassID)
            );

--返回用户是否在有权利的职位ID中
IF EXISTS (
    SELECT 1
    FROM
        dbo.UDS_Staff_In_Position SIP
        INNER JOIN @pAssingPosition AP ON SIP.Position_ID = AP.PositionID 
    WHERE
        SIP.staff_id = @StaffID
)
BEGIN
    SET @AllowAccess = 1;
END

END
GO